package com.fhao.forum.controller;

import com.fhao.forum.common.AppResult;
import com.fhao.forum.common.ResultCode;
import com.fhao.forum.config.AppConfig;
import com.fhao.forum.model.Message;
import com.fhao.forum.model.User;
import com.fhao.forum.services.IMessageService;
import com.fhao.forum.services.IUserService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@ApiResponses
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @ApiResponse(description = "发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                          @RequestParam("content") @NonNull String content) {
        //获取当前登录用户的信息
        HttpSession session = request.getSession(false);
        User postUser = (User) session.getAttribute(AppConfig.USER_SESSION);
        //检查登录用户状态
        if(postUser.getState() == 1) {
            //用户被禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发送
        if(postUser.getId() == receiveUserId) {
            return AppResult.failed(ResultCode.FAILED_CREATE);
        }
        //校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //封装一个对象
        Message message = new Message();
        message.setPostUserId(postUser.getId()); //发送者
        message.setReceiveUserId(receiveUserId); //接收者
        message.setContent(content); //站内信内容
        //调用service
        messageService.create(message);
        //返回结果
        return AppResult.success("发送成功");
    }

    @ApiResponse(description = "获取未读消息个数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request) {
        //获取当前登录用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @ApiResponse(description = "查询用户的所有站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        //拿到当前登录用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        //返回结果
        return AppResult.success(messages);
    }

    @ApiResponse(description = "更新状态为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @RequestParam("id") @NonNull Long id) {
        //获取当前登录用户的信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //根据站内信id,查询站内信详情
        Message message = messageService.selectById(id);
        //校验站内信对象
        if(message == null || message.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS.toString());
        }
        //发送方和接收方不能是同一个
        if(message.getReceiveUserId() != user.getId()) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //调用service
        messageService.updateStateById(id, (byte) 1);
        //返回结果
        return AppResult.success();
    }

    @ApiResponse(description = "回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @RequestParam("repliedId") @NonNull Long repliedId,
                           @RequestParam("content") @NonNull String content) {
        //拿到当前登录用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //拿到站内信id为repliedId的信息
        Message existsMessage = messageService.selectById(repliedId);
        //校验要回复的站内信状态
        if(existsMessage == null || existsMessage.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //不能回复自己的站内信
        if(user.getId() == existsMessage.getPostUserId()) {
            return AppResult.failed("不能回复自己的站内信");
        }
        //封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(existsMessage.getContent());
        //调用service
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}




















